import Http from './http'
import storage from '@/common/js/storage'

export const getVisit = () => {
  const url = 'getVisit'

  return Http.get(url).then((res) => {
    return Promise.resolve(res.data)
  })
}

export const isVisit = ({ account, password }) => {
  const url = 'isVisit'

  const data = Object.assign({}, {
    account,
    password
  })

  return Http.post(url, data).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 用户权限是否失效
export const isVaild = () => {
  const user = storage.session.get('user')

  if (!user) {
    return false
  }

  // 永久有效
  if (user.duration === 0) {
    return true
  }

  // 登录时间与现在时间的差值 秒
  const diffTime = (new Date().getTime() - user.dateLoginNew) / 1000

  if (diffTime > user.duration || diffTime < 0) {
    // 超过有效时间 || 非法时间差
    return false
  } else {
    return true
  }
}
